package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.PartnerDto;
import com.lkd.dto.RegionDto;
import com.lkd.entity.Business;
import com.lkd.entity.Node;
import com.lkd.entity.Region;
import com.lkd.mapper.BusinessMapper;
import com.lkd.mapper.NodeMapper;
import com.lkd.mapper.RegionMapper;
import com.lkd.mapper.VendingMachineMapper;
import com.lkd.result.PageBean;
import com.lkd.service.RegionService;
import com.lkd.vo.NodeVo;
import com.lkd.vo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Override
    public PageBean<RegionVo> findByPage(PartnerDto dto) {
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());

        List<RegionVo> list =regionMapper.fandList(dto.getName());

        Page<RegionVo> page = (Page<RegionVo>) list;


        PageBean<RegionVo> regionVoPageBean = new PageBean<>();
        regionVoPageBean.setPageIndex(page.getPageNum());
        regionVoPageBean.setPageSize(page.getPageSize());
        regionVoPageBean.setTotalPage((long) page.getPages());
        regionVoPageBean.setTotalCount(page.getTotal());
        regionVoPageBean.setCurrentPageRecords(page.getResult());
        return regionVoPageBean;
    }

    @Override
    public Boolean add(RegionDto dto) {
        return regionMapper.add(dto);
    }

    @Override
    public Boolean update(RegionDto dto) {
        return regionMapper.update(dto);
    }

    @Override
    public Boolean deleteById(Long id) {
        return regionMapper.deleteById(id);
    }

    /**
     * 根据区域ID查找节点信息。
     *
     * @param regionDto 区域信息传输对象，包含分页信息和区域ID。
     * @return PageBean<NodeVo> 分页后的节点详细信息对象列表。
     */
    @Override
    public PageBean<NodeVo> findNodeByRegionId(RegionDto regionDto) {
        // 开始分页
        PageHelper.startPage(regionDto.getPageIndex(), regionDto.getPageSize());

        // 查询节点列表
        List<Node> nodeList = nodeMapper.findList(regionDto);
        // 将查询结果转换为分页对象
        Page<Node> page = (Page<Node>) nodeList;

        // 转换节点列表为节点详细信息VO列表
        List<NodeVo> nodeVoList = nodeList.stream().map(node -> {
            // 查询区域信息并转换为区域VO
            Long regionId = node.getRegionId();
            Region region = regionMapper.findById(regionId);
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            // 统计该区域下的节点数量
            Integer count = nodeMapper.countByRegionId(region.getId());
            regionVo.setNodeCount(count);

            // 查询商圈信息
            Integer businessId = node.getBusinessId();
            Business business = businessMapper.findById(businessId);

            // 统计该节点下的机器数量
            Integer vmCount = vendingMachineMapper.countByNodeId(node.getId());

            // 组装节点详细信息VO
            NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);
            nodeVo.setRegion(regionVo);
            nodeVo.setBusinessType(business);
            nodeVo.setVmCount(vmCount);

            return nodeVo;
        }).collect(Collectors.toList());

        // 构建并返回分页信息对象
        PageBean<NodeVo> pageBean = new PageBean<>();
        pageBean.setPageIndex(page.getPageNum());
        pageBean.setPageSize(page.getPageSize());
        pageBean.setTotalPage((long) page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setCurrentPageRecords(nodeVoList);
        return pageBean;
    }


}
